package com.yyq.servlets;

import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.yyq.adms.sysmanage.bpo.impl.SysParametersBpoImpl;

@Controller  
@RemoteProxy(creator=SpringCreator.class)
public class ValidateRepeat {

	@Autowired
	private SysParametersBpoImpl bpo;
	
	 @RemoteMethod  
    public String checkRepeat(String tableName,String condition,String maintainDep) {  
		String sql = "select t.maintainDep,d.dept_cname from "+tableName +" t left join " +
				" ss_sys_depts d on t.maintainDep=d.id where 1=1";
		if(condition != null && !condition.trim().equals("")){
			sql += " and "+condition;
		}
		List list = bpo.searchSql(sql);
		String msg = "";
		if(list != null && list.size()>0){
			sql = "select * from "+tableName+" obj where (obj.maintainDep="+maintainDep+
			" or EXISTS(select * from yyq_record_sentdep rsd where obj.id=rsd.sourcerecordid and rsd.yyq_tablename='"+tableName+"' and rsd.sentdepid="+maintainDep+"))";
			if(condition != null && !condition.trim().equals("")){
				sql += " and "+condition;
			}
			if(bpo.searchSql(sql).size() > 0){
				msg = "{processId:\"2\",msg:\"您已添加这条记录了\"}";
			}else{
				msg = "{processId:\"1\",msg:\"已添加\",deptName:\""+((Object[])list.get(0))[1]+"\"}";
			}
		}else{
			msg = "{processId:\"-1\",msg:\"这条记录还没添加\"}";
		}
		return msg;
    }  
    /*  
    @RequestMapping("/user/add.do")  
    public String addUser(HttpServletRequest request) {  
        System.out.println("this is action method");  
        return "/user/list.jsp";  
    } */ 
}
